<template>
  <div class="course-card" @click="goToCourseDetail">
    <div class="course-image">
      <img :src="course.titleImg" :alt="course.title">
      <div class="course-tag" v-if="course.price === '0.00' || course.price === '0' || course.price === 0">免费</div>
      <div class="course-tag hot" v-else-if="course.recommendWeight > 10">热门</div>
    </div>
    <div class="course-info">
      <h3 class="course-title">{{ course.title }}</h3>
      <div class="course-category">
        <span>分类: {{ getCategoryName(course.categoryId) }}</span>
        <span>教师ID: {{ course.teacherId }}</span>
      </div>
      <div class="course-meta">
        <span class="study-count">学习人数: {{ course.studyPeople }}</span>
        <span class="course-price" v-if="isFree">免费</span>
        <span class="course-price" v-else>{{ formatPrice(course.price) }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { computed } from 'vue'

const router = useRouter()
const props = defineProps({
  course: {
    type: Object,
    required: true
  }
})

// 判断是否是免费课程
const isFree = computed(() => {
  const price = Number(props.course.price)
  return price === 0 || props.course.price === '0' || props.course.price === '0.00'
})

// 格式化价格显示
const formatPrice = (price) => {
  return `￥${Number(price).toFixed(2)}`
}

const getCategoryName = (categoryId) => {
  const categoryMap = {
    1: '学术',
    2: '技术与工程',
    3: '商业与管理',
    4: '艺术与设计',
    5: '职业技能',
    6: '健康与医学',
    7: '语言与文化',
    8: '科技与未来趋势',
    9: '体育与健身'
  }
  return categoryMap[categoryId] || '未知'
}

const goToCourseDetail = () => {
  router.push({
    name: 'CourseDetail',
    params: { id: props.course.courseId }
  })
}
</script>

<style scoped>
.course-card {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.course-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.course-image {
  width: 100%;
  height: 160px;
  position: relative;
  overflow: hidden;
}

.course-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.course-card:hover .course-image img {
  transform: scale(1.05);
}

.course-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 4px 8px;
  background-color: #67c23a;
  color: white;
  border-radius: 4px;
  font-size: 12px;
}

.course-tag.hot {
  background-color: #f56c6c;
}

.course-info {
  padding: 15px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

.course-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 0 0 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.course-category {
  font-size: 13px;
  color: #909399;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
}

.course-meta {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.study-count {
  color: #909399;
  font-size: 13px;
}

.course-price {
  color: #f56c6c;
  font-size: 16px;
  font-weight: 500;
}
</style> 